<template>
  <div class="container">
    <div class="cont-left">
      <div class="create">
        <el-button  @click="linkCreateManage" class="create-btn" type="primary" icon="el-icon-plus"
          >创建档案</el-button
        >
      </div>
      <change-label
        :labelData="labelData"
        @handleLabel="handleLabel"
      ></change-label>
    </div>
    <div class="cont-right">
      <div class="cont-head">
        <div class="search-box">
          <input
            v-model="search"
            class="search"
            type="text"
            placeholder="请输入人名或特征词"
          />
          <i class="el-icon-search" @click="getPerson"></i>
        </div>
        <div class="filter-time" @click="setSort">
          <span class="txt">更新时间</span>
          <div class="icon-box">
            <i
              :class="update_sort == 1 ? 'active' : ''"
              class="icon el-icon-caret-top"
            ></i>
            <i
              :class="update_sort == 2 ? 'active' : ''"
              class="icon el-icon-caret-bottom"
            ></i>
          </div>
        </div>
      </div>
      <info-item
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        :dataList="dataList"
        :type="2"
        @handleLinkId="handleLinkId"
      ></info-item>
      <no-data :length="dataList.length" :loading="loading"></no-data>
      <el-pagination
        class="pagination"
        background
        layout="prev, pager, next"
        :total="total"
        :hide-on-single-page="paginationShow"
        prev-text="上一页"
        next-text="下一页"
        @current-change="handleChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { GetOrganizationRecord, OrganizationFilterRecord } from "@/api/organization";
import { tipErr, tipSuc ,tipWarn} from "@/utils/index";
export default {
  data() {
    return {
      loading: true,
      search: "",
      update_sort: 0, //更新时间排序
      labelData: [],
      labelVal:'',
      labelName:'',
      dataList: [],
      pageSize: 10,
      pageNum: 0,
      total: 0,
    };
  },
  computed: {
    paginationShow() {
      return this.total > 10 ? false : true;
    },
  },
  async mounted() {
    this._GetOrganizationRecord();
    this._OrganizationFilterRecord();
  },
  methods: {
    linkCreateManage() {
      this.$router.push({
        name: "createmanagearchives",
      });
    },
    setSort() {
      if (this.update_sort != 1) {
        this.update_sort = 1;
        this.dataList.sort((a, b) => {
          return a.updateTime - b.updateTime;
        });
      } else if (this.update_sort != 2 && this.update_sort != 0) {
        this.update_sort = 2;
        this.dataList.sort((a, b) => {
          return b.updateTime - a.updateTime;
        });
      }
    },
    async _OrganizationFilterRecord() {
      let { data, code, msg } = await OrganizationFilterRecord({
        create_sort: 0,
        recordScope: 0,
        type: "ORGANIZATION",
      });
      if (code == 1) {
        let arr;
        arr = data.map((item) => {
          let json = {
            name: item.name,
            showAll: false,
          };
          json.list = item.recordContext.map((items) => {
            return { value: items };
          });
          return json;
        });
        this.labelData = [...arr];
      } else {
        tipErr(msg);
      }
    },
    async _GetOrganizationRecord(index, jsons) {
      let params = {
        create_sort: this.create_sort,
        recordScope: 0,
        row: 10,
        start: index ? index * 10 : 0,
        type: "ORGANIZATION",
      };
      if(this.labelName != ''){
       params = Object.assign(params, {[this.labelName]:this.labelVal});
      }
       if(this.search != ''){
       params = Object.assign(params, {searchContent: this.search});
      }
      if (jsons) {
        params = Object.assign(params, jsons);
      }
      let { data, code, msg, total } = await GetOrganizationRecord(params);
      this.loading = false;
      if (code == 1) {
        this.total = total;
        this.dataList = [...data.entityBasicInfoList];
        this.pageNum = index?index+1:this.pageNum + 1
      } else {
        tipErr(msg);
      }
    },
    handleChange(index) {
      this.loading = true;
      this._GetOrganizationRecord((index-1));
    },
    handleLabel(item) {
      this.loading = true
      this.pageNum = 0;
      let name;
      this.search = ''
      if (item.name == "ORG_LABEL") {
        name = "searchLabel";
      } else if (item.name == "ORG_LOCATION_P") {
        name = "searchTerritory";
      }
      this.labelName = name
      this.labelVal = item.value
      this._GetOrganizationRecord(this.pageNum, {
        [name]: item.value,
      });
    },
    getPerson() {
      if(!this.search){
        tipWarn('请输入搜索内容！')
        return
      }
            this.loading = true
      this.pageNum = 0;
      this._GetOrganizationRecord(this.pageNum, {
        searchContent: this.search,
      });
    },
    handleLinkId(item) {
      this.$router.push({
        name: "organizationDetail",
        query: {
          entityId: item.entityId,
          name:item.entityName,
          abstractType:item.abstractType
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped src="@/assets/css/child.scss"></style>
